package com.power.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.constant.BusinessEnum;
import com.power.domain.Member;
import com.power.domain.ProdComm;
import com.power.ex.handle.BussinessException;
import com.power.feign.ProdMemberFeign;
import com.power.mapper.ProdCommMapper;
import com.power.model.Result;
import com.power.service.ProdCommService;
import com.power.vo.ProdCommData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dingchunlin
 * @description 针对表【prod_comm(商品评论)】的数据库操作Service实现
 * @createDate 2024-07-10 15:12:13
 */
@Service
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm>implements ProdCommService {

    @Autowired
    private ProdCommMapper prodCommMapper;

    @Autowired
    private ProdMemberFeign prodMemberFeign;

    /**
     * 回复并审核商品评论
     * @param prodComm 商品评论
     * @return 审核结果
     */
    @Override
    public Boolean replayAndExamineProdComm(ProdComm prodComm) {
        String replyContent = prodComm.getReplyContent();
        if (StringUtils.hasText(replyContent)){
            prodComm.setReplyTime(new Date());
            prodComm.setReplySts(1);
        }
        return prodCommMapper.updateById(prodComm) > 0;
    }

    /**
     * 查询微信端商品评论数据
     * @param prodId 商品ID
     * @return 商品评论数据
     */
    @Override
    public ProdCommData queryWxProdCommDataByProdId(Long prodId) {
        // 查询商品评论数据总数量
        Long allCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1));
        // 查询商品好评数量
        Long goodCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 0)
                .eq(ProdComm::getStatus, 1));
        // 查询商品中评数量
        Long secondCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 1)
                .eq(ProdComm::getStatus, 1));
        // 查询商品差评数量
        Long badCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 2)
                .eq(ProdComm::getStatus, 1));
        // 查询商品有图评论数量
        Long picCount = prodCommMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus,1)
                .isNotNull(ProdComm::getPics));
        // 好评率
        BigDecimal goodLv = BigDecimal.ZERO;
        if (allCount!= 0){
            goodLv = new BigDecimal(goodCount)
                    .divide(new BigDecimal(allCount), 3, RoundingMode.HALF_DOWN)
                    .multiply(new BigDecimal(100));
        }
        // 封装返回结果
        return ProdCommData.builder()
                .allCount(allCount)
                .goodCount(goodCount)
                .secondCount(secondCount)
                .badCount(badCount)
                .picCount(picCount)
                .goodLv(goodLv)
                .build();
    }

    /**
     * 查询微信端商品评论分页数据
     * @param page     分页对象
     * @param prodId   商品ID
     * @param evaluate 查询条件
     * @return 商品评论分页数据
     */
    @Override
    public Page<ProdComm> queryWxProdCommPageByProd(Page<ProdComm> page, Long prodId, Long evaluate) {
        // 查询商品评论分页数据
        page = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1)
                .eq(0==evaluate || 1==evaluate || 2==evaluate, ProdComm::getEvaluate, evaluate)
                .isNotNull(3 == evaluate, ProdComm::getPics)
                .orderByDesc(ProdComm::getCreateTime));
        // 从分页对象中获取数据
        List<ProdComm> prodCommList = page.getRecords();
        // 判断是否有值
        if (CollectionUtils.isEmpty(prodCommList) || prodCommList.size() == 0){
            return page;
        }
        // 从评论集合中获取用户openid集合
        List<String> openIdList = prodCommList.stream().map(ProdComm::getOpenId).collect(Collectors.toList());
        // 远程调用，查询用户集合
        Result<List<Member>> result = prodMemberFeign.getMemberListByOpenIds(openIdList);
        // 判断是否查询成功
        if (result.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
            throw new BussinessException("远程调用：根据会员openid查询会员信息失败！");
        }
        List<Member> memberList = result.getData();
        // 封装用户信息到评论集合
        prodCommList.forEach(prodComm -> {
            Member member = memberList.stream()
                    .filter(m -> m.getOpenId().equals(prodComm.getOpenId()))
                    .collect(Collectors.toList()).get(0);
            // 将会员昵称进行脱敏处理
            StringBuffer stringBuffer = new StringBuffer(member.getNickName());
            stringBuffer.replace(1, stringBuffer.length() - 1, "***");
            prodComm.setNickName(stringBuffer.toString());
            // 将会员头像进行处理
            prodComm.setPic(member.getPic());
        });
        return page;
    }
}




